﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CIRDiagnoseDevice.Test;

namespace CIRDiagnoseDevice.Control
{
    class DealCommandData
    {
        private CommunicationWithInterfaceUnit unit = new CommunicationWithInterfaceUnit();
        public bool flag;
        public  DealCommandData(List<byte> ListBuffer)
        {
            bool flag = false;
            byte[] frameData = unit.ExtractFrame(ListBuffer);
            while (frameData == null)
            {
                frameData = unit.ExtractFrame(ListBuffer);
            }
            if (frameData != null)
            {
                byte[] data = unit.DePacketData(frameData);
                if (MainFrm.MotherFrm.LbjManufName == "深圳长龙")
                {
                    flag = CLResponseCommand(data);
                }
                else if (MainFrm.MotherFrm.LbjManufName == "上海通信")
                {
                    flag = SHResponseCommand(data);
                }
            }
           this.flag=flag;
        }
        # region 处理上海通信Lbj响应命令

        public bool SHResponseCommand(byte[] recdata)
        {
            bool flag = false;
            if (recdata[4] == 0x02)
            {
                if (recdata[7] == 0x4F && recdata[8] == 0x07 && recdata[9] == 0xFF)
                {
                    // 已经进入测试状态
                    flag = true;
                }
                else if (recdata[7] == 0x4F && recdata[8] == 0x07 && recdata[9] == 0xF2)
                {
                    // 已经退出单项测试
                    flag = true;
                }
                else if (recdata[7] == 0x4F && recdata[8] == 0x01 && recdata[9] == 0x05)
                {
                    //已经进入载波容差、发射机功率、调制限制测试状态
                    flag = true;
                }
                else if (recdata[7] == 0x4F && recdata[8] == 0x01 && recdata[9] == 0x06)
                {
                    // 已经进入调制频偏测试状态
                    flag = true;
                }
                else if (recdata[7] == 0x4F && recdata[8] == 0x01 && recdata[9] == 0x01)
                {
                    // 已经进入接收机指标测试状态
                    flag = true;
                }
            }
            return flag;
        }

        # endregion

        # region 处理长龙Lbj响应命令

        public bool CLResponseCommand(byte[] recdata)
        {
            bool flag = false;
            if (recdata[4] == 0x02)
            {
                if (recdata[7] == 0x4F && recdata[8] == 0x4A && recdata[9] == 0xFF)
                {
                    // 已经进入测试状态
                    flag = true;
                }
                else if (recdata[7] == 0x4F && recdata[8] == 0x4A && recdata[9] == 0xF1)
                {
                    // 已经进入单项测试状态
                    flag = true;
                }
                else if (recdata[7] == 0x4F && recdata[8] == 0x4A && recdata[9] == 0xF2)
                {
                    // 已经退出单项测试状态
                    flag = true;
                }
                else if (recdata[7] == 0x4F && recdata[8] == 0x4A && recdata[9] == 0x00)
                {
                    // 已经退出测试状态
                    flag = true;
                }
            }
            return flag;
        }

        # endregion
    }
}
